package dorm
import(
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"os"
	"strings"
	"fmt"
)

/**
desc 表数据库信息
 */
type filedinfo struct{
	Field string
	Type string
	Null string
}
type tableinfo struct{
	tabinfo []filedinfo
}
func getConfig() *Config{
	c,err:=SetConfig("src/config/config.ini")
	checkErr(err)
	return c
}
func Dbinit1() string{
    c:=getConfig()
	var dbuser string =c.Getvalue("database","mysqluser")
	var dbpassword string=c.Getvalue("database","mysqlpass")
	var dbip string=c.Getvalue("database","mysqlip")
	var dbport string=c.Getvalue("database","mysqlport")
	var dbname string=c.Getvalue("database","mysqldb")
	var dbchart string="utf8"
	return dbuser+":"+dbpassword+"@tcp("+dbip+":"+dbport+")/"+dbname+"?charset="+dbchart

}
func GetTablelistAndFile()([]string,string){
	c:=getConfig()
	var table string =c.Getvalue("file","tablelist")
	tableslist:=strings.Split(table,",")
	var pre string =c.Getvalue("file","prepath")
	return tableslist,pre
}
/**
func dbinit()string{
    var dbuser string ="root"
	var dbpassword string=""
	var dbip string="127.0.0.1"
	var dbport string="3306"
	var dbname string="test"
	var dbchart string="utf8"
	return dbuser+":"+dbpassword+"@tcp("+dbip+":"+dbport+")/"+dbname+"?charset="+dbchart
}
**/

func Te1(tablename string)tableinfo{
	dns:=Dbinit1()
	db, err := sql.Open("mysql", dns)
	checkErr(err)
	myerr:=db.Ping()
	if myerr!=nil{
		panic("error")
	}
	rows2, err := db.Query("desc "+tablename)//获取对应表的基本字段信息
	va2,_:=rows2.Columns() //返回数据库字段数组
	lenth:=len(va2)//获取数据库字段个数
	values:= make([]sql.RawBytes, lenth)
	scanArgs:= make([]interface{}, len(values))
	for i := range values {
		scanArgs[i] = &values[i]
	}
	var tab tableinfo
	for rows2.Next() {
		err = rows2.Scan(scanArgs...)
		if err != nil {
			panic(err.Error())
		}
		var infomation filedinfo
		infomation.Field=string(values[0])
		infomation.Type=string(values[1])
		infomation.Null=string(values[2])
		/**
		var value string
		for i, col := range values {
			if col == nil {
				value = "NULL"
			} else {
				value = string(col)
			}

			fmt.Println(va2[i], ": ", value)
		}
		**/
		tab.tabinfo=append(tab.tabinfo,infomation)
	}
	return tab
}
/**
自动生成model 文件
 */
func AutotoolRun(){
	tables,pre:=GetTablelistAndFile()
	var filedtype string
	for _,va:=range tables{
		path:=pre+strings.Title(va)+".go"
		_, err := os.Stat(path)
		if err == nil {//以前的model文件存在则删除
			err=os.Remove(path)
			if err!=nil{
				panic("error")
			}
		}
		f,err:=os.Create(path)
		if err!=nil{
			panic("error")
		}
		v:=Te1(va)
		defer  f.Close()
		f.WriteString("package model\n\n\n")
		f.WriteString("type "+strings.Title(va)+" struct{ \n")

		for _,va:=range v.tabinfo {
			if strings.HasPrefix(va.Type,"char") || strings.HasPrefix(va.Type,"varchar"){
				filedtype="string"
			}else if strings.HasPrefix(va.Type,"int") || strings.HasPrefix(va.Type,"tinyint") || strings.HasPrefix(va.Type,"smallint")||strings.HasPrefix(va.Type,"mediumint") || strings.HasPrefix(va.Type,"integer")||strings.HasPrefix(va.Type,"bigint") {
				filedtype="int"
			}else if strings.HasPrefix(va.Type,"double") || strings.HasPrefix(va.Type,"float"){
				filedtype="float32"
			}else{
				filedtype="string"
			}
			f.WriteString("\t"+strings.Title(va.Field)+"\t\t"+filedtype+"\n")
		}
		f.WriteString("}\n")
	}
}


func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

func CheckErr(err error) string {
	if err != nil {
		return fmt.Sprintf("Error is :'%s'", err.Error())
	}
	return "Notfound this error"
}
